﻿

using Ccteg.Common;
using Ccteg.Common.QueryModels.DncPermission;

using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;

namespace Ccteg.DB
{
    /// <summary>
    /// 
    /// </summary>
    public class MySqlDbContext : DbContext
    {


        /// <summary>
        /// 
        /// </summary>
        /// <param name="options"></param>
        public MySqlDbContext(DbContextOptions<MySqlDbContext> options) : base(options)
        {

        }
        /// <summary>
       


        public DbSet<tsk_jobtask> tsk_jobtask { get; set; }
        /// <summary>
        /// 系统配置数据
        /// </summary>
        public DbSet<Management_data> Management_data { get; set; }

        /// <summary>
        /// 工程项目
        /// </summary>
        public DbSet<srb_main_project> Srb_Main_Project { get; set; }

        /// <summary>
        /// 托管煤矿
        /// </summary>
        public DbSet<srb_main_mine> Srb_Main_Mine { get; set; }

        /// <summary>
        /// 自有煤矿
        /// </summary>
        public DbSet<srb_main_mine_self> Srb_Main_Mine_Self { get; set; }


        /// <summary>
        /// 加工基地
        /// </summary>
        public DbSet<srb_main_processingbase> Srb_Main_Processingbase { get; set; }

        /// <summary>
        /// 法律法规
        /// </summary>
        public DbSet<law_law> Law_Law { get; set; }

        /// <summary>
        /// 安全工作管理
        /// </summary>
        public DbSet<tar_tar> Tar_Tar { get; set; }


        /// <summary>
        /// 安全组织机构
        /// </summary>
        public DbSet<org_org> ORG_Org { get; set; }

        /// <summary>
        /// 安全文化
        /// </summary>
        public DbSet<Scl_Scl> SCL_Scl { get; set; }

        /// <summary>
        /// 安全会议
        /// </summary>
        public DbSet<Scf_Scf> SCF_Scf { get; set; }

        /// <summary>
        /// 企业信息
        /// </summary>

        public DbSet<ent_baseinfo> Ent_Baseinfo { get; set; }
        public virtual DbSet<ent_baseinfo_ent> Ent_baseinfo_Ent { get; set; } //跨库查询
        /// <summary>
        /// 安全风险业务清单
        /// </summary>
        public DbSet<srb_main_risk> srb_main_risk { get; set; }



        /// <summary>
        /// 安全风险业务措施
        /// </summary>
        public DbSet<srb_main_risk_measure> srb_main_risk_measure { get; set; }
        public DbSet<cctegaq_test> cctegaq_test { get; set; }

        /// <summary>
        /// 风险管控
        /// </summary>
        public DbSet<rsk_rsk> Rsk_Rsk { get; set; }

        /// <summary>
        /// 风险管控---管控措施
        /// </summary>
        public DbSet<rsk_measure> Rsk_Measure { get; set; }

        #region DbQuery
        /// <summary>
        /// 
        /// </summary>

        public DbQuery<DncPermissionWithAssignProperty> DncPermissionWithAssignProperty { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public DbQuery<DncPermissionWithMenu> DncPermissionWithMenu { get; set; }
        #endregion

        /// <summary>
        /// EF变更日志
        /// </summary>
        private void WriteEFDataLog()
        {
            //获取到EF变更条目
            var list = this.ChangeTracker.Entries();
            foreach (var item in list)
            {
                //对应的表名
                string tableName = "";

                #region 获取表名
                Type type = item.Entity.GetType();
                Type patientMngAttrType = typeof(TableAttribute);
                TableAttribute attribute = null;
                if (type.IsDefined(patientMngAttrType, true))
                {
                    attribute = type.GetCustomAttributes(patientMngAttrType, true).FirstOrDefault() as TableAttribute;
                    if (attribute != null)
                    {
                        tableName = attribute.Name;
                    }
                }

                if (string.IsNullOrEmpty(tableName))
                {
                    tableName = type.Name;
                }
                #endregion

                switch (item.State)
                {
                    case EntityState.Detached:

                        break;
                    case EntityState.Unchanged:

                        break;
                    case EntityState.Deleted:

                        break;
                    case EntityState.Modified:
                        // WriteEFUpdateLog(item, tableName);
                        var item1 = item;
                        var cc = tableName;
                        break;
                    case EntityState.Added:

                        break;
                }
            }
        }

        //public override int SaveChanges(bool efLogFlag=false)
        //{


        //    if (efLogFlag)
        //    {
        //        WriteEFDataLog();
        //    }

        //    return base.SaveChanges();
        //}
        /// <summary>
        /// 
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //modelBuilder.Entity<DncUser>()
            //    .Property(x => x.Status);
            //modelBuilder.Entity<DncUser>()
            //    .Property(x => x.IsDeleted);


            modelBuilder.Entity<DncRole>(entity =>
            {
                entity.HasIndex(x => x.Code).IsUnique();
            });

            modelBuilder.Entity<DncMenu>(entity =>
            {
                //entity.haso
            });


            modelBuilder.Entity<DncUserRoleMapping>(entity =>
            {
                entity.HasKey(x => new
                {
                    x.UserGuid,
                    x.RoleCode
                });

                entity.HasOne(x => x.DncUser)
                    .WithMany(x => x.UserRoles)
                    .HasForeignKey(x => x.UserGuid)
                    .OnDelete(DeleteBehavior.Restrict);

                entity.HasOne(x => x.DncRole)
                    .WithMany(x => x.UserRoles)
                    .HasForeignKey(x => x.RoleCode)
                    .OnDelete(DeleteBehavior.Restrict);
            });

            modelBuilder.Entity<DncPermission>(entity =>
            {
                entity.HasIndex(x => x.Code)
                    .IsUnique();

                entity.HasOne(x => x.Menu)
                    .WithMany(x => x.Permissions)
                    .HasForeignKey(x => x.MenuGuid);
            });

            modelBuilder.Entity<DncRolePermissionMapping>(entity =>
            {
                entity.HasKey(x => new
                {
                    x.RoleCode,
                    x.PermissionCode
                });

                entity.HasOne(x => x.DncRole)
                    .WithMany(x => x.Permissions)
                    .HasForeignKey(x => x.RoleCode)
                    .OnDelete(DeleteBehavior.Restrict);

                entity.HasOne(x => x.DncPermission)
                    .WithMany(x => x.Roles)
                    .HasForeignKey(x => x.PermissionCode)
                    .OnDelete(DeleteBehavior.Restrict);
            });
            modelBuilder.Entity<ent_baseinfo_ent>().ToTable("ent_baseinfo", "cctegaq");

            
            modelBuilder.Entity<Scl_Scl>(e => { e.HasQueryFilter(x => x.Deleted == false); });
            modelBuilder.Entity<Scf_Scf>(e => { e.HasQueryFilter(x => x.Deleted == false); });
            modelBuilder.Entity<law_law>(e => { e.HasQueryFilter(x => x.Deleted == false); });
            modelBuilder.Entity<srb_main_risk>(e => { e.HasQueryFilter(x => x.Deleted == false); });
            modelBuilder.Entity<srb_main_risk_measure>(e => { e.HasQueryFilter(x => x.Deleted == false); });
            modelBuilder.Entity<rsk_rsk>(e => { e.HasQueryFilter(x => x.Deleted == false); });
            modelBuilder.Entity<rsk_measure>(e => { e.HasQueryFilter(x => x.Deleted == false); });
            modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
            base.OnModelCreating(modelBuilder);

        }
    }
}



//e.HasQueryFilter(x => !x.Deleted);
//modelBuilder.Entity<Scl_Scl>(e =>
//{
//    e.HasOne(x => x.ent_baseinfo_ent).WithOne(x => x.Scl_Scl_No).HasForeignKey<Scl_Scl>(x => x.CompanyNo);
//   // e.HasQueryFilter(x => !x.Deleted);
//});